﻿using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using ThridGroup.ERP.DTO.TransferTransfersDto;
using ThridGroup.ERP.WarehouseManagement;

namespace ThridGroup.ERP.Web.Controllers.WarehouseManagement
{
    [Route("WarehouseManagement/TransferTransfers")]
    public class TransferTransfersController : Controller
    {
        private readonly ITransferTransfersServices _transferTransfersServices;

        public TransferTransfersController(ITransferTransfersServices transferTransfersServices)
        {
            _transferTransfersServices = transferTransfersServices;
        }

        /// <summary>
        /// 转移调拨主页面    ---tsh
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [HttpGet("Index")]
        public IActionResult Index()
        {
            return View("~/Views/WarehouseManagement/TransferTransfersView.cshtml");
        }
        
        /// <summary>
        /// 转移调拨视图页面（兼容旧路由）
        /// </summary>
        /// <returns></returns>
        [HttpGet("TransferTransfersView")]
        public IActionResult TransferTransfersView()
        {
            return View("~/Views/WarehouseManagement/TransferTransfersView.cshtml");
        }

        /// <summary>
        /// 获取转移调拨列表
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns></returns>
        [HttpGet("GetList")]
        public async Task<IActionResult> GetList([FromQuery] QueryTransferTransfersDto query)
        {
            try
            {
                var result = await _transferTransfersServices.GetListAsync(query);
                return Json(new
                {
                    code = 0,
                    msg = "查询成功",
                    count = result.total,
                    data = result.data
                });
            }
            catch (System.Exception ex)
            {
                return Json(new
                {
                    code = 1,
                    msg = $"查询失败：{ex.Message}",
                    count = 0,
                    data = new object[] { }
                });
            }
        }

        /// <summary>
        /// 根据ID获取转移调拨
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <returns></returns>
        [HttpGet("GetById/{id}")]
        public async Task<IActionResult> GetById(int id)
        {
            try
            {
                var result = await _transferTransfersServices.GetByIdAsync(id);
                return Json(new
                {
                    code = 0,
                    msg = "查询成功",
                    data = result
                });
            }
            catch (System.Exception ex)
            {
                return Json(new
                {
                    code = 1,
                    msg = $"查询失败：{ex.Message}"
                });
            }
        }

        /// <summary>
        /// 创建转移调拨
        /// </summary>
        /// <param name="input">创建输入</param>
        /// <returns></returns>
        [HttpPost("Create")]
        public async Task<IActionResult> Create([FromBody] CreateTransferTransfersDto input)
        {
            try
            {
                var result = await _transferTransfersServices.CreateAsync(input);
                return Json(new
                {
                    code = 0,
                    msg = "创建成功",
                    data = result
                });
            }
            catch (System.Exception ex)
            {
                return Json(new
                {
                    code = 1,
                    msg = $"创建失败：{ex.Message}"
                });
            }
        }

        /// <summary>
        /// 更新转移调拨
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <param name="input">更新输入</param>
        /// <returns></returns>
        [HttpPut("Update/{id}")]
        public async Task<IActionResult> Update(int id, [FromBody] CreateTransferTransfersDto input)
        {
            try
            {
                var result = await _transferTransfersServices.UpdateAsync(id, input);
                return Json(new
                {
                    code = 0,
                    msg = "更新成功",
                    data = result
                });
            }
            catch (System.Exception ex)
            {
                return Json(new
                {
                    code = 1,
                    msg = $"更新失败：{ex.Message}"
                });
            }
        }

        /// <summary>
        /// 删除转移调拨
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <returns></returns>
        [HttpDelete("Delete/{id}")]
        public async Task<IActionResult> Delete(int id)
        {
            try
            {
                await _transferTransfersServices.DeleteAsync(id);
                return Json(new
                {
                    code = 0,
                    msg = "删除成功"
                });
            }
            catch (System.Exception ex)
            {
                return Json(new
                {
                    code = 1,
                    msg = $"删除失败：{ex.Message}"
                });
            }
        }

        /// <summary>
        /// 生成转移单编号
        /// </summary>
        /// <returns></returns>
        [HttpGet("GenerateCode")]
        public async Task<IActionResult> GenerateCode()
        {
            try
            {
                var code = await _transferTransfersServices.GenerateTransferCodeAsync();
                return Json(new
                {
                    code = 0,
                    msg = "生成成功",
                    data = code
                });
            }
            catch (System.Exception ex)
            {
                return Json(new
                {
                    code = 1,
                    msg = $"生成失败：{ex.Message}"
                });
            }
        }
    }
}
